package com.learning.spring.ioc.life;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * ClassName: LifeCycleBean
 * Description:
 * Date: 2018/8/3 13:13 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class LifeCycleBean implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, ApplicationContextAware {

    private String name;

    public LifeCycleBean() {
        System.out.println("调用 LifeCycleBean 构造函数");
    }

    public void setName(String name) {
        System.out.println("设置 setName 属性");
        this.name = name;
    }

    /**
     * BeanFactoryAware 接口方法
     *
     * @param beanFactory
     * @throws BeansException
     */
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("调用 BeanFactoryAware.setBeanFactory() 方法");
    }

    /**
     * BeanNameAware 接口方法
     *
     * @param name
     */
    @Override
    public void setBeanName(String name) {
        System.out.println("调用 BeanNameAware.setBeanName() 方法");
    }

    /**
     * InitializingBean 接口方法
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用 InitializingBean.afterPropertiesSet() 方法");
    }

    /**
     * DisposableBean 接口方法
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("调用 DisposableBean.destroy() 方法");
    }

    /**
     * init-method 指定的初始化方法
     */
    public void init() {
        System.out.println("init-method 指定的初始化方法");
    }


    /**
     * destroy-method 指定的销毁方法
     */
    public void customDestroy() {
        System.out.println("destroy-method 指定的销毁方法");
    }


    /**
     * ApplicationContextAware 接口，ApplicationContext 下文文容器
     *
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("调用 ApplicationContextAware.setApplicationContext() 方法");
    }
}
